package com.kld.simplelauncher.app;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.util.Log;


import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.kld.simplelauncher.utils.EntryActivity;

import java.util.Collections;
import java.util.List;

public class MoreAppPageAdapter extends FragmentPagerAdapter {
    private static final String TAG = "MoreAppPageAdapter";
    private List<EntryActivity> allAppList;

    public MoreAppPageAdapter(@NonNull FragmentManager fm, List<EntryActivity> allAppList) {
        super(fm);
        this.allAppList = allAppList;
    }


    @NonNull
    @Override
    public Fragment getItem(int position) {
        int startPosition = position * 10;
        int endPosition = position * 10 + 10;

        if (allAppList != null && !allAppList.isEmpty()) {
            List<EntryActivity> pageInfoList = null;
            if (allAppList.size() > startPosition && allAppList.size() >= endPosition) {
                pageInfoList = allAppList.subList(startPosition,endPosition);
                Log.d(TAG, "getItem: size = " + pageInfoList.size());
            } else if (allAppList.size() > startPosition) {
                pageInfoList = allAppList.subList(startPosition,allAppList.size());
                Log.d(TAG, "getItem: size = " + pageInfoList.size());
            }
            if (pageInfoList != null && !pageInfoList.isEmpty()) {
                return new ShowAppFragment(position,pageInfoList);
            }
        }
        return null;
    }

    @Override
    public int getCount() {
        if (allAppList != null && !allAppList.isEmpty()) {
            int pageCount = (allAppList.size() / 10) + (allAppList.size() % 10 != 0?1:0);
            Log.d(TAG, "getItemCount: size = " + allAppList.size() + "   pageCount = " + pageCount);
            return pageCount;
        }
        return 0;
    }

    public void setAllAppList(List<EntryActivity> allAppList) {
        this.allAppList = allAppList;
        notifyDataSetChanged();
    }
}
